home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 9.9 KB | 377 lines | [TEXT/KAHL] |
- /***************************************************** IMPLEMENTATION
- DATE: 9/20/93
- AUTHOR: Eric R. Rosé
-
- CLASS: CPPMenuBar
-
- SUPERCLASS: CPPObject
-
- This C++ class manages a Macintosh menu bar
-
- ********************************************************************/
-
- #pragma once
-
- #include <CPPMenuBar.h>
- #include <CPPWindowManager.h>
- #include <CPPWindow.h>
- #include <CPPApplication.h>
- #include <ToolboxTools.h>
-
- typedef struct {
- short menuID;
- short itemID;
- short commandID;
- } MenuBinding;
- typedef MenuBinding *BindingPtr;
-
- extern CPPWindowManager *gWindowManager;
- extern CPPApplication *gApplication;
-
- /*-----------------------------------------------------------------*/
- /*------------------------ PUBLIC METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPMenuBar::CPPMenuBar (short ResID, short appleID) : CPPList()
- /* create a menu bar based on the passed-in resource template */
- /* and the ID of the apple menu */
- {
- ClearMenuBar();
- this->appleMenu = NULL;
- this->appleMenuID = appleID;
- this->theMenuBar = GetNewMBar (ResID);
- if (this->theMenuBar)
- SetMenuBar (this->theMenuBar);
- DrawMenuBar();
- SetCommandBindings();
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPMenuBar::CPPMenuBar (void) : CPPList ()
- /* create a menu bar which has only the apple menu in it */
- {
- ClearMenuBar();
- this->theMenuBar = GetMenuBar();
- this->appleMenu = NewMenu (1, "\p\024" );
- this->appleMenuID = 1;
- AppendMenu( appleMenu, "\pAbout…");
- AddDAs (appleMenu);
- InsertMenu( appleMenu, 0 ); /* install in menu bar */
- DrawMenuBar ();
- SetCommandBindings();
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPMenuBar::~CPPMenuBar (void)
- {
- if (this->appleMenu)
- DisposeMenu(this->appleMenu);
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPMenuBar::Member (char *className)
- {
- if (strcmp(className, CPPMenuBar::ClassName()) == 0)
- return TRUE;
- else
- return CPPList::Member(className);
- }
-
- /*-----------------------------------------------------------------*/
-
- char *CPPMenuBar::ClassName (void)
- {
- return "CPPMenuBar";
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::DoMenu (long menuResult)
- {
- short menuID = HiWord(menuResult);
- short menuItem = LoWord(menuResult);
-
- DoMenu (menuID, menuItem);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::SetUpHierMenus (short startID, short endID)
- /* load any hierarchical menus */
- {
- MenuHandle TempMenu;
-
- for (short i = startID; i <= endID; i++)
- {
- TempMenu = GetMenu (i);
- if (TempMenu)
- InsertMenu (TempMenu, -1);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::DoMenu (short menuID, short theItem)
- /* figure out what command goes with the chosen item */
- /* and send it to the front window or the application */
- {
- long commandID = MenuToCommand (menuID, theItem);
- CPPWindow *theWindow;
-
- if (commandID)
- {
- // send the command to the front window (if any)
- if (gWindowManager &&
- (theWindow = gWindowManager->FrontWindowObject()))
- {
- if (!theWindow->DoCommand(commandID))
- gApplication->DoCommand(commandID);
- }
- else
- // otherwise, send the command to the application
- gApplication->DoCommand(commandID);
- }
- else
- {
- if (appleMenu || (menuID == this->appleMenuID))
- DoAppleMenuItem (this->appleMenuID, theItem);
- }
- HiliteMenu(0); // turn menu selection off
- }
-
- /*-----------------------------------------------------------------*/
-
- MenuHandle CPPMenuBar::MenuFromID (short ResID)
- /* return the handle of the menu whose ID is ResID */
- {
- return GetMHandle (ResID);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::BindCommand (short MenuID, short ItemID, short CommandID)
- /* add an entry in the command list for the specified menu and item */
- {
- BindingPtr TempBinding = (BindingPtr)NewPtrClear(sizeof(MenuBinding));
-
- if (TempBinding)
- {
- TempBinding->menuID = MenuID;
- TempBinding->itemID = ItemID;
- TempBinding->commandID = CommandID;
- AppendItem((Ptr)TempBinding);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::CheckCommand (short CommandID, Boolean doCheck)
- /* put or remove a checkmark from the menu item specified by CommandID */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- CheckItem(theMenu, ItemID, doCheck);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::EnableCommand (short CommandID)
- /* enable the menu item specified by CommandID */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- EnableItem(theMenu, ItemID);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::DisableCommand (short CommandID)
- /* disable the menu item specified by CommandID */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- DisableItem(theMenu, ItemID);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::MarkCommand (short CommandID, char markChar)
- /* mark the menu item specified by CommandID with markChar */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- SetItemMark(theMenu, ItemID, markChar);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::SetCommandText (short CommandID, StringPtr theString)
- /* set the name of the menu item specified by CommandID */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- SetMenuItemText(theMenu, ItemID, theString);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::SetCommandIcon (short CommandID, short theIconID)
- /* set the icon for the menu item specified by CommandID */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- SetItemIcon(theMenu, ItemID, theIconID);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::SetCommandStyle (short CommandID, Style newStyle)
- /* set the style of the text for the menu item specified by CommandID */
- {
- MenuHandle theMenu;
- short ItemID;
-
- if (CommandToMenu (CommandID, &theMenu, &ItemID))
- SetItemStyle(theMenu, ItemID, newStyle);
- }
-
- /*-----------------------------------------------------------------*/
- /*---------------------- PROTECTED METHODS ------------------------*/
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::AddDAs (MenuHandle theMenu)
- /* call this to add the 'apple menu' list to the menu with the */
- /* specified resource ID */
- {
- AppendMenu( theMenu, "\p(-" ); /* dotted line */
- AddResMenu( theMenu, 'DRVR' ); /* Add DA list */
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::AddDAs (short ResID)
- /* call this to add the 'apple menu' list to the menu with the */
- /* specified resource ID */
- {
- MenuHandle tempMenu = MenuFromID (ResID);
-
- if (tempMenu)
- {
- AppendMenu( tempMenu, "\p(-" ); /* dotted line */
- AddResMenu( tempMenu, 'DRVR' ); /* Add DA list */
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPMenuBar::CommandToMenu (short commandID,
- MenuHandle *theMenu,
- short *itemID)
- /* convert a command to a handle to its menu and an item in the menu*/
- /* return TRUE if the menu handle exists */
- {
- short menuID;
-
- if (CommandToMenuAndItem (commandID, &menuID, itemID))
- {
- if ((*theMenu = MenuFromID (menuID)) != NULL)
- return TRUE;
- else
- return FALSE;
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- short CPPMenuBar::MenuToCommand (short menuID, short itemID)
- /* return the command ID associated with the given menu and item */
- {
- BindingPtr theBinding;
- for (long i = 1; i <= this->numItems; i++)
- {
- theBinding = (BindingPtr)((*this)[i]);
- if (theBinding)
- {
- if ((theBinding->menuID == menuID) &&
- (theBinding->itemID == itemID))
- return (theBinding->commandID);
- }
- }
-
- // if we don't find a binding, return 0
- return 0;
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPMenuBar::CommandToMenuAndItem (short commandID, short *menuID,
- short *itemID)
- /* return the menu and item ID associated with the given command */
- {
- BindingPtr theBinding;
-
- for (long i = 1; i <= this->numItems; i++)
- {
- theBinding = (BindingPtr)((*this)[i]);
- if (theBinding)
- {
- if (theBinding->commandID == commandID)
- {
- *menuID = theBinding->menuID;
- *itemID = theBinding->itemID;
- return TRUE;
- }
- }
- }
-
- // if we don't find the command, return FALSE;
- return FALSE;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::DoAppleMenuItem (short appleMenuID, short theItem)
- {
- short dna;
- Str255 daName;
- GrafPtr savePort;
- MenuHandle theAppleMenu = GetMHandle (appleMenuID);
-
- if (theAppleMenu)
- {
- GetPort(&savePort); // Save the current port
- GetItem(theAppleMenu, theItem, daName);// get DA Name
- dna = OpenDeskAcc(daName); // Open the DA
- SetPort(savePort); // Restore to the saved port
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPMenuBar::SetCommandBindings (void)
- /* Set all of the application specific bindings for command */
- /* numbers to menu and item numbers */
- /* SUBCLASS SHOULD OVERRIDE */
- {
-
- }
-
- /*-----------------------------------------------------------------*/
-
-
-